<template>
  <el-form ref="formRef" :model="form" label-width="120px" size="default">
    <el-form-item>
      <h2>
        <el-link type="warning">按试管标阴性。</el-link>
        <br />
        <el-link type="danger">审核人不必填，不填写不生成auditer与auditer_account字段</el-link>
      </h2>
    </el-form-item>
    <el-form-item label="试管号：">
      <el-input v-model="form.number" clearable class="input-width"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="add">添加</el-button>
      <el-button type="danger" @click="reset">重置</el-button>
    </el-form-item>
    <el-form-item label="">
      {{ JSON.stringify(form.numbers) }}
    </el-form-item>
    <el-form-item label="审核人：">
      <el-input v-model="form.reviewer" clearable class="input-width"></el-input>
    </el-form-item>
    <el-form-item label="审核时间：">
      <el-date-picker
        v-model="form.reviewTime"
        type="datetime"
        :editable="false"
        placeholder="选择日期时间"
        format="YYYY-MM-DD HH:mm:ss"
        value-format="YYYY-MM-DD HH:mm:ss"
        @change="reviewTimeChange"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item label="减八小时：">
      <el-date-picker
        v-model="form.completeTime"
        :clearable="false"
        :readonly="true"
        type="datetime"
        :editable="false"
        placeholder="选择审核时间自动计算"
        format="YYYY-MM-DD HH:mm:ss"
        value-format="YYYY-MM-DD HH:mm:ss"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">生成脚本</el-button>
      <el-button type="default" @click="exportFile">导出文件</el-button>
    </el-form-item>
    <el-form-item label="脚本：">
      <el-input autosize type="textarea" v-model="form.scriptString" readonly></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, reactive } from "vue";
import { format } from "./js/timeFromat";
import saveAs from "@/views/script-generator/js/FileSaver";
// do not use same name with ref
const form = reactive({
  // 试管号
  number: "",
  numbers: [],
  // 审核人
  reviewer: "",
  // 审核时间
  reviewTime: "",
  // 审核时间减八小时
  completeTime: "",
  // 脚本
  scriptString: ""
});

const add = () => {
  form.number.trim() ? form.numbers.push(form.number.trim()) : "";
  form.number = "";
};

const reset = () => {
  form.number = "";
  form.numbers = [];
};

const exportFile = () => {
  let str = new Blob([form.scriptString], {
    type: "text/plain;charset=utf-8"
  });
  saveAs(str, `人按照试管标阴性，试管号为${form.numbers.toString()}.txt`);
};

const getReviewer = () => {
  let str = `
tubeObj.auditer = '${form.reviewer}';
tubeObj.auditer_account = 'shenheren';`;
  return form.reviewer ? str : "";
};

const reviewTimeChange = () => {
  let date = new Date(form.reviewTime.replaceAll("-", "/"));
  form.completeTime = format(date.setHours(date.getHours() - 8));
};

const onSubmit = () => {
  form.scriptString = `环境：太极政务云
数据库地址：192.159.107.33:2000
数据库名：nat_admin

先备份（请将备份的数据写在jira的备注里，如果数据多，可以生成txt文件上传到jira里）
var c = db.nat_tube.find(
{"tube_code":{"$in":${JSON.stringify(form.numbers)}}}
)
d = []
while(c.hasNext())
{ d.push(c.next()) }
printjson(d)

再执行
db.nat_tube.find({$and:[{"tube_code":{"$in":${JSON.stringify(
    form.numbers
  )}}}]}).forEach(function (tubeObj) {${getReviewer()}
tubeObj.status = NumberInt(5);
tubeObj.check_result = NumberInt(0);
tubeObj.modify_time = '${form.reviewTime}';
tubeObj.audit_time = '${form.reviewTime}';
tubeObj.completed_time = ISODate('${form.completeTime.slice(0, 10)}T${form.completeTime.slice(
    11
  )}.000Z');db.getCollection('nat_tube').save(tubeObj);
var appoCheckList = db.getCollection('nat_appo_check').find({"tube_id": tubeObj.id,"abnormal_sign":{$ne:1}}).
forEach( function ( appoCheckObj) {
db.getCollection('nat_appo_check').update({"tube_id":tubeObj.id,id:appoCheckObj.id},{$set:{
status : NumberInt(6),
check_result_time:tubeObj.audit_time,
modify_time: tubeObj.modify_time,
audit_time : tubeObj.audit_time,
completed_time : tubeObj.completed_time,
check_result : NumberInt(0)}},false,true);
var relationObj = db.getCollection('nat_appo_check_relation').findOne({"appo_check_id": appoCheckObj.id,"abnormal_sign":{$ne:1}});
if(relationObj != null){
relationObj.appo_status = NumberInt(6);
relationObj.check_result = NumberInt(0);
relationObj.check_result_time = tubeObj.audit_time;
relationObj.update_user_name = 'sys';
relationObj.auditer = tubeObj.auditer;
relationObj.auditer_account = tubeObj.auditer_account;
relationObj.modify_time = tubeObj.modify_time;
relationObj.audit_time = tubeObj.audit_time;
relationObj.completed_time = tubeObj.completed_time;
relationObj.sam_inst_id = tubeObj.sam_inst_id;
relationObj.check_inst_id = tubeObj.check_inst_id;
relationObj.check_date = tubeObj.check_time.substring(0, 10);
relationObj.check_time = tubeObj.check_time;
relationObj.sam_dev_code = tubeObj.sam_dev_code;
relationObj.belongto_checkinst_id = tubeObj.belongto_checkinst_id;
relationObj.check_user_name = tubeObj.check_user_name;
if (appoCheckObj.sam_id != null) {
var samPointInfo = db.getCollection('nat_inst_sample_point').findOne({"id": appoCheckObj.sam_id});
if (samPointInfo != null) {
if (samPointInfo.sample_point_name != null) {
relationObj.sample_point_name = samPointInfo.sample_point_name
}
if (samPointInfo.sample_point_type != null) {
relationObj.sample_point_type = samPointInfo.sample_point_type;
}
}
}
if (tubeObj.sam_inst_id != null) {
var samInstInfo = db.getCollection('nat_institution').findOne({"id": tubeObj.sam_inst_id});
if (samInstInfo != null) {
if (samInstInfo.inst_name != null) {
relationObj.sam_inst_name = samInstInfo.inst_name;
}
}
}
if (tubeObj.check_inst_id != null) {
var checkInstInfo = db.getCollection('nat_institution').findOne({"id": tubeObj.check_inst_id});
if (checkInstInfo != null) {
if (checkInstInfo.inst_name != null) {
relationObj.check_inst_name = checkInstInfo.inst_name;
}
}
}
relationObj.completed_time = tubeObj.completed_time;
relationObj.check_result_sync_flag = NumberInt(1);
relationObj.report_path = "/opt/azero/nat-gen-report/data/" + appoCheckObj.inst_id + "/" + tubeObj.audit_time.substring(0, 10) + "/" + appoCheckObj.checked_person_card_number + ".pdf";
db.getCollection('nat_appo_check_relation').save(relationObj);
}else{
print(appoCheckObj.id+'无中间表信息');
var relationInfo = new Object();
if (appoCheckObj.id != null) {
relationInfo.appo_check_id = appoCheckObj.id;
}
if (appoCheckObj.tube_id != null) {
relationInfo.tube_id = appoCheckObj.tube_id;
}
if (appoCheckObj.checked_person_name != null) {
relationInfo.checked_person_name = appoCheckObj.checked_person_name;
}
if (appoCheckObj.tube_code != null) {
relationInfo.tube_code = appoCheckObj.tube_code;
}
if (appoCheckObj.sam_id != null) {
relationInfo.samp_id = appoCheckObj.sam_id;
}
if (appoCheckObj.status != null) {
relationInfo.appo_status = appoCheckObj.status;
}
if (appoCheckObj.sam_time != null) {
relationInfo.sam_time = appoCheckObj.sam_time;
}
if (appoCheckObj.appo_type != null) {
relationInfo.check_type = appoCheckObj.appo_type;
}
if (appoCheckObj.card_number_type != null) {
relationInfo.card_number_type = appoCheckObj.card_number_type;
}
if (appoCheckObj.checked_person_card_number != null) {
relationInfo.checked_person_card_number = appoCheckObj.checked_person_card_number;
}
if (appoCheckObj.checked_person_telephone != null) {
relationInfo.checked_person_telephone = appoCheckObj.checked_person_telephone;
}
if (appoCheckObj.checked_person_live_address != null) {
relationInfo.checked_person_live_address = appoCheckObj.checked_person_live_address;
}
if (appoCheckObj.checked_person_detail_address != null) {
relationInfo.checked_person_detail_address = appoCheckObj.checked_person_detail_address;
}
if (appoCheckObj.payment_fees != null) {
relationInfo.payment_fees = appoCheckObj.payment_fees;
}
if (appoCheckObj.payment_time != null) {
relationInfo.payment_time = appoCheckObj.payment_time;
}
if (appoCheckObj.sam_id != null) {
var samPointInfo = db.getCollection('nat_inst_sample_point').findOne({"id": appoCheckObj.sam_id});
if (samPointInfo != null) {
if (samPointInfo.sample_point_name != null) {
relationInfo.sample_point_name = samPointInfo.sample_point_name
}
if (samPointInfo.sample_point_type != null) {
relationInfo.sample_point_type = samPointInfo.sample_point_type;
}
}
}
if (tubeObj.sam_inst_id != null) {
relationInfo.sam_inst_id = tubeObj.sam_inst_id;
}
if (tubeObj.sam_inst_id != null) {
var samInstInfo = db.getCollection('nat_institution').findOne({"id": tubeObj.sam_inst_id});
if (samInstInfo != null) {
if (samInstInfo.inst_name != null) {
relationInfo.sam_inst_name = samInstInfo.inst_name;
}
}
}
if (tubeObj.check_inst_id != null) {
relationInfo.check_inst_id = tubeObj.check_inst_id;
}
if (tubeObj.check_inst_id != null) {
var checkInstInfo = db.getCollection('nat_institution').findOne({"id": tubeObj.check_inst_id});
if (checkInstInfo != null) {
if (checkInstInfo.inst_name != null) {
relationInfo.check_inst_name = checkInstInfo.inst_name;
}
}
}
if (tubeObj.sam_dev_code != null) {
relationInfo.sam_dev_code = tubeObj.sam_dev_code;
}
if (tubeObj.belongto_checkinst_id != null) {
relationInfo.belongto_checkinst_id = tubeObj.belongto_checkinst_id;
}
if (tubeObj.check_time != null && tubeObj.check_time.length >= 10) {
relationInfo.check_date = tubeObj.check_time.substring(0, 10);
}
if (tubeObj.check_time != null) {
relationInfo.check_time = tubeObj.check_time;
}
if (tubeObj.check_result_time != null) {
relationInfo.check_result_time = tubeObj.check_result_time;
}
if (tubeObj.check_user_name != null) {
relationInfo.check_user_name = tubeObj.check_user_name;
}
if (tubeObj.check_result != null) {
relationInfo.check_result = tubeObj.check_result;
}
relationInfo.check_result_sync_flag = NumberInt(1);
relationInfo.report_path = "/opt/azero/nat-gen-report/data/" +
appoCheckObj.inst_id + "/" + tubeObj.audit_time.substring(0, 10) + "/" +
appoCheckObj.checked_person_card_number + ".pdf";
db.getCollection('nat_appo_check_relation').save(relationInfo);
}
});
});`;
};
</script>

<style>
.input-width {
  width: 500px;
}
</style>
